/**
 Gulpfile for gulp-webpack-demo
 created by fwon
 */

var gulp = require('gulp'),
	os = require('os'),
	gulpOpen = require('gulp-open'),
	clean = require('gulp-clean'),
	webpack = require('webpack'),
	webpackConfig = require('./webpack.config.js'),
	connect = require('gulp-connect');

var watch = require('gulp-watch');
var nodemon = require('gulp-nodemon');
var browserSync = require('browser-sync');
var reload = browserSync.reload;


var gulp_webpack = require('gulp-webpack');

//mac chrome: "Google chrome", 
var browser = os.platform() === 'linux' ? 'Google chrome' : (
	os.platform() === 'darwin' ? 'Google chrome' : (
		os.platform() === 'win32' ? 'chrome' : 'firefox'));

gulp.task('clean', function (done) {
	return gulp.src([
		'./public/assets/',
		'./public/css/',
		'./public/img/',
		'./public/js/',
	])
		.pipe(clean());
});

//将图片拷贝到目标目录
gulp.task('copy:images', ['clean'], function (done) {
	return gulp.src([
		'src/img/*',
		'src/img/**/*.jpg',
	])
		.pipe(gulp.dest('public/img'))
});

gulp.task('watch', function (done) {
	gulp.watch([
		'src/js/*.js',
		'src/js/**/*.js',
		'src/js/**/**/*.js',
		'src/css/*.less',
		'src/css/**/*.less',
		'src/css/**/**/*.less',
		'src/css/**/*.css',
		'src/css/*.css',
		'src/component/*.html',
		'src/component/**/*.html',
	], ['build-js'])
		.on('end', done);
});

gulp.task('node', function (done) {
	nodemon({
		script: 'keystone.js',
		ext: 'js html',
		ignore: [
			'public/**/*',
			'src/**/*'
		],
		env: {
			'NODE_ENV': process.env.NODE_ENV,
			'DEBUG_FD': false
		}
	});
	done();
		// .on('start', done)
		// .on('restart', function (files) {
		// 	console.log('restart-changeFile',files);
		// });
});

gulp.task('dev-serve', ['node'], function (done) {
	var files = [
		// 'src/js/*.js',
		// 'src/js/**/*.js',
		// 'src/css/*.less',
		// 'src/css/**/*.less',
		// 'src/css/**/*.css',
		// 'src/css/*.css',
		// 'src/img/*.*',
		// 'src/img/**/*.*',
		'router/*.js',
		'router/**/*.js',
		// 'public/**/*.*',
		// 'templates/*.hbs',
		// 'templates/**/*.hbs',
	];

	browserSync.init(files, {
		proxy: 'http://localhost:3000',
		browser: 'chrome',
		notify: false,
		port: 3001,
		open: false,
	});
	gulp.watch(files)
		.on("change", reload);
	done();

});

gulp.task('open', ['watch'], function (done) {
	gulp.src('')
		.pipe(gulpOpen({
			app: browser,
			uri: 'http://0.0.0.0:3000',
		}))
		.on('end', done);
});

//引用webpack对js进行操作
gulp.task("build-js", function (done) {
	var myDevConfig = Object.create(webpackConfig);
	var devCompiler = webpack(myDevConfig);
	// devCompiler.run(function(err, stats) {
	//     if(err) throw new gutil.PluginError("webpack:build-js", err);
	//     gutil.log("[webpack:build-js]", stats.toString({
	//         colors: true
	//     }));
	//     callback();
	// });
	gulp_webpack(myDevConfig, webpack)
		.pipe(gulp.dest('./build/'))
		.on('end', done);
});

//发布
gulp.task('default', ['clean', 'copy:images', 'build-js']);

//开发
// gulp.task('dev', ['dev-serve', 'copy:images', 'lessmin', 'build-js', 'watch', 'open']);
gulp.task('dev', ['dev-serve']);


